{% extends "layout_default.phtml" %}
{% block meta_title %}Product promos{% endblock %}
{% set active_menu = 'products' %}

{% block breadcrumbs %}
<ul>
    <li class="firstB"><a href="{{ '/'|alink }}">{% trans 'Home' %}</a></li>
    <li><a href="{{ 'product'|alink }}">{% trans 'Products' %}</a></li>
    <li><a href="{{ 'product/promos'|alink }}">{% trans 'Product promotions' %}</a></li>
    <li class="lastB">{{ promo.code }}</li>
</ul>
{% endblock %}

{% block content %}
<div class="widget">

    <div class="head">
        <h5>{% trans 'Manage coupon code' %}</h5>
    </div>

    <form method="post" action="{{ 'api/admin/product/promo_update'|link }}" class="mainForm save api-form" data-api-redirect="{{ 'product/promos'|alink }}">
        <fieldset>
            <div class="rowElem noborder">
                <label>{% trans 'Code' %}</label>
                <div class="formRight">
                    <input type="text" name="code" value="{{ promo.code }}" required="required"/>
                </div>
                <div class="fix"></div>
            </div>
            <div class="rowElem">
                <label>{% trans 'Discount type' %}</label>
                <div class="formRight moreFields">
                    <ul>
                        <li style="width: 270px">
                            <input type="radio" name="type" value="absolute" {% if promo.type == 'absolute' %}checked="checked"{% endif %}/><label>Absolute</label>
                            <input type="radio" name="type" value="percentage" {% if promo.type == 'percentage' %}checked="checked"{% endif %}/><label>Percentage</label>
                        </li>
                        <li style="width: 100px"><input type="text" name="value" value="{{ promo.value }}" required="required"/></li>
                    </ul>
                </div>
                <div class="fix"></div>
            </div>
            
            <div class="rowElem">
                <label>{% trans 'Recurring' %}</label>
                <div class="formRight moreFields">
                    <input type="radio" name="recurring" value="0" {% if not promo.recurring %}checked="checked"{% endif %}/><label>Applies to first order only</label>
                    <input type="radio" name="recurring" value="1" {% if promo.recurring %}checked="checked"{% endif %}/><label>Applies to first order and renewals</label>
                </div>
                <div class="fix"></div>
            </div>
            
            <div class="rowElem">
                <label>{% trans 'Active' %}</label>
                <div class="formRight">
                    <input type="radio" name="active" value="1" {% if promo.active %}checked="checked"{% endif %}/><label>Yes</label>
                    <input type="radio" name="active" value="0" {% if not promo.active %}checked="checked"{% endif %}/><label>No</label>
                </div>
                <div class="fix"></div>
            </div>
            
            <div class="rowElem">
                <label>{% trans 'Free setup' %}</label>
                <div class="formRight">
                    <input type="radio" name="freesetup" value="1" {% if promo.freesetup %}checked="checked"{% endif %}/><label>Yes</label>
                    <input type="radio" name="freesetup" value="0" {% if not promo.freesetup %}checked="checked"{% endif %}/><label>No</label>
                </div>
                <div class="fix"></div>
            </div>
        </fieldset>

        <fieldset>
            <legend>{% trans %}Promo code limitations{% endtrans %}</legend>
            
            <div class="rowElem">
                <label>{% trans 'Once per client' %}</label>
                <div class="formRight">
                    <input type="radio" name="once_per_client" value="1" {% if promo.once_per_client %}checked="checked"{% endif %}/><label>{% trans 'Yes' %}</label>
                    <input type="radio" name="once_per_client" value="0" {% if not promo.once_per_client %}checked="checked"{% endif %}/><label>{% trans 'No' %}</label>
                </div>
                <div class="fix"></div>
            </div>
            
            <div class="rowElem">
                <label>{% trans 'Max uses (zero for unlimited)' %}</label>
                <div class="formRight">
                    <input type="text" name="maxuses" value="{{ promo.maxuses }}" class="dirRight" title="Leave blank for unlimited uses"  placeholder="0"/>
                </div>
                <div class="fix"></div>
            </div>
            <div class="rowElem">
                <label>{% trans 'Used' %}</label>
                <div class="formRight">
                    <input type="text" name="used" value="{{ promo.used }}" class="dirRight" title="How many times promo code was used"  placeholder="0"/>
                </div>
                <div class="fix"></div>
            </div>
            <div class="rowElem">
                <label>{% trans 'Period when code is valid' %}</label>
                <div class="formRight moreFields">
                    <ul>
                        <li style="width: 100px"><input type="text" name="start_at" value="{% if promo.start_at %}{{ promo.start_at|date('Y-m-d') }}{%endif%}" placeholder="{{ now|date('Y-m-d') }}" class="datepicker"/></li>
                        <li class="sep">-</li>
                        <li style="width: 100px"><input type="text" name="end_at" value="{% if promo.end_at %}{{ promo.end_at|date('Y-m-d') }}{%endif%}" placeholder="{{ now|date('Y-m-d') }}" class="datepicker"/></li>
                    </ul>
                </div>
                <div class="fix"></div>
            </div>
            <div class="rowElem">
                <label>{% trans 'Products' %}</label>
                <div class="formRight">
                    {% set products = admin.product_get_pairs %}
                    <input type="hidden" name="products" value="">
                    <select name="products[]" multiple="multiple" class="multiple" size="{{products|length}}">
                        {% for id,product in products %}
                        <option value="{{id}}" {% if id in promo.products %}selected="selected"{% endif %}>{{ product }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="fix"></div>
            </div>
            <div class="rowElem">
                <label>{% trans 'Periods (Select none to apply to all periods)' %}</label>
                <div class="formRight">
                    {% set periods = guest.system_periods %}
                    <input type="hidden" name="periods" value="">
                    <select name="periods[]" multiple="multiple" class="multiple" size="{{periods|length}}">
                        {% for id,period in periods %}
                        <option value="{{id}}" {% if id in promo.periods %}selected="selected"{% endif %}>{{ period }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="fix"></div>
            </div>
            <div class="rowElem">
                <label>{% trans 'Client Groups (Select none to apply to all client groups)' %}</label>
                <div class="formRight">
                    {% set client_groups = admin.client_group_get_pairs %}
                    <input type="hidden" name="client_groups" value="">
                    <select name="client_groups[]" multiple="multiple" class="multiple" size="{{groups|length}}">
                        {% for id, client_group in client_groups %}
                        <option value="{{id}}" {% if id in promo.client_groups %}selected="selected"{% endif %}>{{ client_group }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="fix"></div>
            </div>
            <input type="submit" value="{% trans 'Update' %}" class="greyishBtn submitForm" />
            <input type="hidden" name="id" value="{{ promo.id }}" />
        </fieldset>
    </form>
</div>

{% endblock %}
